# -*- coding: utf-8 -*-
import numpy as np
from neo4j import *


def solve_floor_count_room(entity):
    """
    统计某层馆室数量
    """
    floor = entity['floor'][0]
    res = get_reverse_relation(floor, '馆室')
    ans = '\n' + floor + '一共有' + str(len(res)) + '间馆室\n'
    return ans


def solve_floor_room_a(entity):
    """
    查出楼层是否有某一间馆室
    """
    floor = entity['floor'][0]
    res = get_reverse_relation(floor, '馆室')
    ans = '\n' + floor + '有馆室'
    for r in res[:-1]:
        room_name = r['office_name']
        if room_name.find(u'_') != -1:
            room_name = room_name.split('_')[2]
        ans += room_name + ','
    room_name = res[-1]['office_name']
    if room_name.find(u'_') != -1:
        room_name = room_name.split('_')[2]
        ans += room_name + '\n'

    return ans


def solve_room_floor(entity):
    """
    查出楼层所有的馆室
    """
    floor = entity['floor'][0]
    room = entity['room'][0]
    res = get_relation(room, '楼层')
    ans = '\n'
    room_name = room
    if room.find('_') != -1:
        room_name = room.split('_')[2]
    if res[0]['office_name'] == floor:
        ans += room_name + '在' + floor
    else:
        ans += room_name + '不在' + floor + '\n' + room_name + '在' + res[0][
            'office_name'] + '\n'
    return ans


def solve_res_floor(entity):
    """
    处理资源是否在某个楼层的问题，查询资源所在的馆室以及馆室所在的楼层来得到判断结果
    """
    resource = entity['res'][0]
    floor = entity['floor'][0]
    res = get_relation(resource, '馆室')
    room_name = res[0]['office_name']
    if room_name.find('_') != -1:
        room_name = res[0]['office_name'].split('_')[2]
    des_floor = get_relation(res[0]['office_name'], '楼层')
    ans = '\n'
    if floor == des_floor[0]['office_name']:
        ans += resource + '存放在' + floor
        ans += solve_count_res(entity)
    else:
        ans += resource + '不存放在' + floor + '\n' + resource + '存放在' + \
               des_floor[0]['office_name'] + '的' + room_name
    return ans


def solve_res_floor_a(entity):
    """
    楼层包括什么资源，通过查询楼层包含馆室的资源类型来汇总得到楼层包含的资源类型
    """
    floor = entity['floor'][0]
    res = get_reverse_relation(floor, '馆室')
    resource_arr = []
    for r in res:
        res_arr = get_reverse_relation(r['office_name'], '资源')
        for sub_r in res_arr:
            resource_arr.append(sub_r['belong'])

    resource_arr = np.unique(resource_arr)

    ans = '\n'
    ans += floor + '存放有'
    for sub_r in resource_arr[:-1]:
        ans += sub_r + ','
    ans += resource_arr[-1] + '等类型的资源\n'
    return ans


def solve_res_room(entity):
    """
    查出馆室是否有某个资源
    """
    room = entity['room'][0]

    resource = entity['res'][0]
    res = get_relation(resource, '馆室')
    ans = '\n'
    if res[0]['office_name'] == room:
        ans += resource + '存放在' + room
        ans += solve_count_res(entity)
    else:
        des_room = res[0]['office_name']
        if res[0]['office_name'].find(u'_') != -1:
            des_room = res[0]['office_name'].split('_')[2]
        ans += '%s不存放在%s\n%s存放在%s\n' \
               % (resource, room, resource, des_room)
        # ans += resource + '不存放在' + room + '\n' +
        # resource + '存放在' + des_room + '\n'
    return ans


def solve_restype_room(entity):
    """
    查出馆室是否有某种资源的
    """

    room = entity['room'][0]
    restype = entity['restype'][0]
    res = get_relation_mul(restype, '馆室')
    ans = '\n'
    room_arr = [r['office_name'] for r in res]
    print(room_arr)

    if room in room_arr:
        ans += room + '存有' + restype
        ans += solve_count_res(entity)
    else:
        ans += restype + '不存放在' + room + '\n' + restype + '存放在'
        for r in room_arr[:-1]:
            ans += r + ','
        ans += room_arr[-1] + '\n'

    return ans


def solve_restype_area(entity):
    """
    查出馆区是否有某种资源的
    """
    print(entity)
    ans = '\n'
    area = entity['area'][0]

    restype = entity['restype'][0]

    resource = get_reverse_relation(restype, '资源')
    resource_arr = [x['office_name'] for x in resource]
    ans += restype + '包括'
    for sub_res in resource_arr:
        ans += sub_res + ','
    res = get_relation_triple(restype, '馆区')

    area_arr = [r['office_name'] for r in res]

    if area in area_arr:
        ans += area + '存有' + restype + '\n'

    else:
        ans += restype + '不存放在' + area + '\n' + restype + '存放在'
        for r in area_arr[:-1]:
            ans += r + ','
        ans += area_arr[-1] + '\n'

    return ans


def solve_restype_library(entity):
    """
    查出国图是否有某种资源的
    """
    res = get_relation(GraphBaseConfig['place'], '馆区')
    ans = ''
    entity['area'] = [res[0]['office_name']]
    temp = solve_restype_area(entity)
    if '不' in temp.split('\n')[1]:
        pass
    else:
        ans += temp.split('\n')[1]
        return ans
    for r in res[1:]:
        # area_dict = {'area': [r['office_name']]}
        entity['area'] = [r['office_name']]
        temp = solve_restype_area(entity)
        if '不' in temp.split('\n')[1]:
            pass
        else:
            ans += temp.split('\n')[1]
            return ans

    # ans = ''
    # entity['area'] = ['总馆北区']
    # temp = solve_restype_area(entity)
    # ans += temp.split('\n')[0]
    # if '不' in temp.split('\n')[1]:
    #     pass
    # else:
    #     ans += temp.split('\n')[1]
    #     return ans
    # entity['area'] = ['总馆南区']
    # temp = solve_restype_area(entity)
    # if '不' in temp.split('\n')[1]:
    #     pass
    # else:
    #     ans += temp.split('\n')[1]
    #     return ans
    # 
    # entity['area'] = ['文津楼']
    # temp = solve_restype_area(entity)
    # if '不' in temp.split('\n')[1]:
    #     pass
    # else:
    #     ans += temp.split('\n')[1]
    #     return ans
    # 
    # entity['area'] = ['临琼楼']
    # temp = solve_restype_area(entity)
    # if '不' in temp.split('\n')[1]:
    #     pass
    # else:
    #     ans += temp.split('\n')[1]
    #     return ans

    return ans


def solve_mul_type_area(entity):
    """
    查出馆区是否有某种资源的（中级）
    """
    area = entity['area'][0]
    mul_type = entity['mul_type'][0]
    restype_res = get_reverse_relation(mul_type, '资源类型')

    restype_arr_origin = [x['office_name'] for x in restype_res]
    restype_arr = restype_arr_origin
    ans = mul_type + '包括'

    for restype in restype_arr[:-1]:
        ans += restype + ','

    ans += restype_arr[-1] + '\n'
    yes_restype = []
    for restype in restype_arr:
        res = get_relation_triple(restype, '馆区')
        area_arr = [x['office_name'] for x in res]
        if area in area_arr:
            yes_restype.append(restype)
    if len(yes_restype) > 0:

        ans += '其中'
        for r in yes_restype[:-1]:
            ans += r + ','
        ans += yes_restype[-1] + '在' + area + '\n'
    return ans


def solve_t_type_area(entity):
    """
    查看某馆区时候有什么大类资源
    """
    area = entity['area'][0]
    t_type = entity['t_type'][0]
    restype_res = get_reverse_relation(t_type, '资源类型')

    restype_arr_origin = [x['office_name'] for x in restype_res]
    restype_arr = restype_arr_origin
    ans = t_type + '包括'

    for restype in restype_arr[:-1]:
        ans += restype + ','

    ans += restype_arr[-1] + '\n'
    yes_restype = []
    for restype in restype_arr:
        res = get_relation_triple(restype, '馆区')
        area_arr = [x['office_name'] for x in res]
        if area in area_arr:
            yes_restype.append(restype)
    if len(yes_restype) > 0:

        ans += '其中'
        for r in yes_restype[:-1]:
            ans += r + ','
        ans += yes_restype[-1] + '在' + area + '\n'
    else:
        ans += area + '没有' + t_type + '\n'
    return ans


def solve_mul_type_library(entity):
    """
    查看国图有么有某中类
    """
    res = get_relation(GraphBaseConfig['place'], '馆区')
    ans = ''
    entity['area'] = [res[0]['office_name']]
    ans += solve_mul_type_area(entity)
    for r in res[1:]:
        # area_dict = {'area': [r['office_name']]}
        entity['area'] = [r['office_name']]
        temp = solve_mul_type_area(entity)
        temp = temp.split('\n')[1]
        ans += temp + '\n'
    return ans
    # ans = ''
    # entity['area'] = ['总馆北区']
    # ans += solve_mul_type_area(entity)
    # entity['area'] = ['总馆南区']
    # temp = solve_mul_type_area(entity)
    # temp = temp.split('\n')[1]
    # ans += temp+'\n'
    # entity['area'] = ['文津楼']
    # temp = solve_mul_type_area(entity)
    # temp = temp.split('\n')[1]
    # ans += temp+'\n'
    # entity['area'] = ['临琼楼']
    # temp = solve_mul_type_area(entity)
    # temp = temp.split('\n')[1]
    # ans += temp
    # return ans


def solve_t_type_library(entity):
    """
    查看国图有么有某中类
    """
    res = get_relation(GraphBaseConfig['place'], '馆区')
    ans = ''
    entity['area'] = [res[0]['office_name']]
    ans += solve_t_type_area(entity)
    for r in res[1:]:
        # area_dict = {'area': [r['office_name']]}
        entity['area'] = [r['office_name']]
        temp = solve_t_type_area(entity)
        temp = temp.split('\n')[1]
        ans += temp + '\n'
    #     ans = ''
    #     entity['area'] = ['总馆北区']
    #     ans += solve_t_type_area(entity)
    #     entity['area'] = ['总馆南区']
    #     temp = solve_t_type_area(entity)
    #     temp = temp.split('\n')[1]
    #     ans += temp+'\n'
    #     entity['area'] = ['文津楼']
    #     temp = solve_t_type_area(entity)
    #     temp = temp.split('\n')[1]
    #     ans += temp+'\n'
    #     entity['area'] = ['临琼楼']
    #     temp = solve_t_type_area(entity)
    #     temp = temp.split('\n')[1]
    #     ans += temp
    #     return ans
    return ans


def solve_res_library(entity):
    """
    查看国图有么有某中类
    """
    res = entity['res'][0]
    ans = GraphBaseConfig['place'] + '的' + res + ',存放在'
    room = get_relation(res, '馆室')
    room_arr = [x['office_name'] for x in room]
    for room_name in room_arr[:-1]:
        ans += room_name + ','
    ans += room_arr[-1] + '\n'
    return ans


def solve_goods_library(entity):
    goods = entity['goods'][0]
    ans = GraphBaseConfig['place'] + '有' + goods + ',存放在'
    room = get_relation(goods, '馆室')
    room_arr = [x['office_name'] for x in room]
    for room_name in room_arr[:-1]:
        ans += room_name + ','
    ans += room_arr[-1] + '\n'
    return ans


def solve_mul_type_room(entity):
    """
    查出馆室是否有某种资源的（中级）
    """
    room = entity['room'][0]
    mul_type = entity['mul_type'][0]
    restype_search = get_reverse_relation(mul_type, '资源类型')
    restype_arr = [x['office_name'] for x in restype_search]
    ans = '\n'
    if len(restype_arr) > 0:
        ans += mul_type + '包括'
    else:
        return '很抱歉，没有' + mul_type + '的具体信息\n'
    for restype in restype_arr[:-1]:
        ans += restype + ','
    ans += restype_arr[-1] + '\n'
    # print('===',restype_arr)
    for restype in restype_arr:
        # print(restype)
        res = get_relation_mul(restype, '馆室')
        # print(res)

        room_arr = [r['office_name'] for r in res]
        # print(room_arr)

        if room in room_arr:
            ans += room + '存有' + restype
            return ans

    ans += mul_type + '不存放在' + room + '\n'
    return ans


def solve_room_res_a(entity):
    """
    处理馆室所有资源的问题
    """
    room = entity['room'][0]
    room_name = room
    if room.find(u'_') != -1:
        room_name = room.split('_')[2]
    res = get_reverse_relation(room, '资源')
    res_dict = {}
    for r in res:
        res_dict[r['belong']] = []
    res_type = list(res_dict.keys())
    # print(res_type)
    if not res_type:
        return room_name + '不存放任何资源\n'
    ans = '\n'
    ans += room_name + '存放的资源类型包括：'
    for t in res_type[:-1]:
        ans += t + ','
    ans += res_type[-1] + '\n'

    for r in res:
        res_dict[r['belong']].append(r['office_name'])
    for t in res_type:
        ans += t + '：'
        for r in res_dict[t][:-1]:
            ans += r + ','
        ans += res_dict[t][-1] + '\n'
    return ans


def solve_count_res(entity):
    """
    资源数量问题
    """
    resource = entity['res'][0]
    res = get_property(resource)
    ans = '\n' + resource + '存放在'
    room = get_relation(resource, '馆室')
    room_arr = [x['office_name'] for x in room]
    # print(room)

    for room_name in room_arr[:-1]:
        ans += room_name + ','
    ans += room_arr[-1] + '\n'

    if res['count'] != 'nan':
        ans += resource + '的数量是:' + str(res['count']) + '本（份）\n'
    else:
        ans += '很抱歉，' + resource + '暂时没有数据信息\n'
    return ans


def solve_count_restype(entity):
    """
    某一类资源数量问题
    """
    restype = entity['restype'][0]
    # res = get_property(restype)
    ans = '\n'
    res_type = get_property(restype)

    if res_type['count'] != 'nan':
        ans += restype + '的数量是:' + str(int(float(res_type['count']))) + '本（份）\n'
    else:
        ans += '很抱歉，' + restype + '暂时没有数据信息\n'
    return ans


def solve_count_mul_type(entity):
    mul_type = entity['mul_type'][0]
    restype_search = get_reverse_relation(mul_type, '资源类型')
    restype_arr = [x['office_name'] for x in restype_search]
    ans = '\n'
    if len(restype_arr) > 0:
        ans += mul_type + '包括'
    else:
        return '很抱歉，没有' + mul_type + '的具体信息\n'
    for restype in restype_arr[:-1]:
        ans += restype + ','
    ans += restype_arr[-1] + '\n'
    for restype in restype_arr:
        entity['restype'] = [restype]
        ans += solve_count_restype(entity)[1:]
    return ans


def solve_res_res_h(entity):
    """
    某资源是否是某类资源问题
    """
    restype = entity['restype'][0]
    resource = entity['res'][0]
    res_arr = []
    res = get_reverse_relation(restype, '资源')
    ans = '\n'
    for r in res[:-1]:
        res_arr.append(r['office_name'])
    if resource in res_arr:

        ans += '是的,' + resource + '属于' + restype + '\n'
    else:
        ans += '很抱歉,' + resource + '不属于' + restype + '\n'
    return ans


def solve_mul_res_h(entity):
    """
    某资源是否是某类资源问题
    """
    restype = entity['restype'][0]
    mul_type = entity['mul_type'][0]
    res_arr = []
    res = get_reverse_relation(mul_type, '资源类型')
    ans = '\n'
    for r in res[:-1]:
        res_arr.append(r['office_name'])
    if restype in res_arr:

        ans += '是的,' + restype + '属于' + mul_type + '\n'
    else:
        ans += '很抱歉,' + restype + '不属于' + mul_type + '\n'
    return ans


def solve_res_res_a(entity):
    """
    某类资源包含的所有资源
    """
    restype = entity['restype'][0]
    res = get_reverse_relation(restype, '资源')
    if len(res) <= 0:
        return '很抱歉，暂时没有' + restype + '下属资源的信息\n'
    ans = '\n' + restype + '包含的资源有：\n'
    for r in res[:-1]:
        ans += r['office_name'] + ','
    ans += res[-1]['office_name'] + '\n'
    return ans


def solve_mul_res_a(entity):
    """
    某类资源包含的所有资源
    """
    mul_type = entity['mul_type'][0]
    res = get_reverse_relation(mul_type, '资源类型')
    ans = '\n' + mul_type + '包含的资源有:'
    for r in res[:-1]:
        ans += r['office_name'] + ','
    ans += res[-1]['office_name'] + '\n'
    return ans


def solve_res_res_t(entity):
    """
    资源类型（资源的中级类别）属于哪一大类问题（资源的最高类别）
    """
    restype = entity['restype'][0]
    res = get_property(restype)
    ans = '\n' + restype + '属于' + res['belong']
    return ans


def solve_count_floor(entity):
    """
    馆室有几楼的
    """
    area = entity['area'][0]
    res = get_reverse_relation(area, '楼层')
    # print(res)
    ans = '\n' + area + '一共有' + str(len(res)) + '层\n'
    return ans


def solve_count_floor_library():
    """
    图书馆有几楼的
    """
    res = get_relation(GraphBaseConfig['place'], '馆区')
    ans = ''
    for r in res:
        area_dict = {'area': [r['office_name']]}
        ans += solve_count_floor(area_dict)
    return ans


def solve_library_area():
    """
    国图有几个馆区
    """
    res = get_relation(GraphBaseConfig['place'], '馆区')
    print(res)
    ans = '\n' + GraphBaseConfig['place'] + '包括'
    for r in res[:-1]:
        ans += r['office_name'] + ','
    ans += res[-1]['office_name'] + '\n'
    return ans


def solve_library_res_a():
    """
    国图包含哪些资源类型，查出所有的资源类别
    """
    area = get_entity('馆区')
    area_arr = [sub_area['office_name'] for sub_area in area]
    ans = GraphBaseConfig['place'] + '各个馆舍在资源和服务方面各有侧重。\n'

    for sub_area in area_arr:
        resource_arr = get_area_resource_type(sub_area)
        if len(resource_arr) > 0:
            ans += sub_area + ': '
            for r in resource_arr[:-1]:
                # print(r)
                ans += r['office_name'] + ','
            ans += resource_arr[-1]['office_name'] + '\n'
    return ans


def solve_service_exit(entity):
    """
    国图是否提供某一服务
    """
    # print(entity)
    service = entity['service']
    # res = get_property(service)
    ans = '\n'
    if len(service) > 0:
        ans += GraphBaseConfig['place'] + '提供' + service[0] + '\n'
        # print('aaa',ans)
        room = get_relation(service[0], '馆室')
        if len(room) <= 0:
            return ans
        ans += '您可以去'
        for r in room[:-1]:
            ans += r['office_name'] + ','
        ans += room[-1]['office_name'] + '接受该服务\n'
    else:
        ans += '很抱歉，' + GraphBaseConfig['place'] + '不提供该服务'
    return ans


def solve_task_exit(entity):
    """
    国图是否提供某一业务
    """
    # print(entity)
    service = entity['task']
    # res = get_property(service)
    ans = '\n'
    if len(service) > 0:
        ans += GraphBaseConfig['place'] + '提供' + service[0] + '\n'
        # print('aaa',ans)
        room = get_relation(service[0], '馆室')
        if len(room) <= 0:
            return ans
        ans += '您可以去'
        for r in room[:-1]:
            ans += r['office_name'] + ','
        ans += room[-1]['office_name'] + '办理该业务\n'
    else:
        ans += '很抱歉，' + GraphBaseConfig['place'] + '不能办理该业务'
    return ans


def solve_service_exit_all():
    """
    国图提供什么服务
    """
    res = get_entity('服务')
    ans = '\n' + GraphBaseConfig['place'] + '提供的服务包括'
    for sub_result in res[:-1]:
        ans += sub_result['office_name'] + ','
    ans += res[-1]['office_name'] + '\n'
    return ans


def solve_service_area_all(entity):
    area = entity['area'][0]
    res = get_reverse_relation_mul(area, '服务')
    ans = '\n'
    if len(res) > 0:
        ans += area + '提供的服务包括'
        for r in res[:-1]:
            ans += r['office_name'] + ','
        ans += res[-1]['office_name'] + '\n'
    else:
        ans += area + '不提供任何服务\n'
    return ans


def solve_library_res():
    """
    查出国图资源分布，即查出国图所有馆区所包含的资源
    """
    res = get_relation(GraphBaseConfig['place'], '馆区')
    ans = ''
    for r in res:
        area_dict = {'area': [r['office_name']]}
        ans += solve_area_res_a(area_dict)
    return ans


def solve_area_res_a(entity):
    """'
    馆区包含什么资源类型
    """
    area = entity['area'][0]
    res = get_reverse_relation(area, '馆室')
    resource_arr = []
    for r in res:
        res_arr = get_reverse_relation(r['office_name'], '资源')
        for sub_r in res_arr:
            resource_arr.append(sub_r['belong'])

    resource_arr = np.unique(resource_arr)

    ans = '\n'
    ans += area + '存放有'
    for sub_r in resource_arr[:-1]:
        ans += sub_r + ','
    ans += resource_arr[-1] + '等类型的资源\n'
    return ans


def solve_service_room_all(entity):
    """
    馆室有什么服务
    """
    room = entity['room'][0]
    res = get_reverse_relation(room, '服务')
    ans = '\n'
    if len(res) > 0:
        ans += room + '提供的服务包括'
        for r in res[:-1]:
            ans += r['office_name'] + ','
        ans += res[-1]['office_name'] + '\n'
    else:
        ans += room + '不提供任何服务\n'
    return ans


def solve_service_room_exit(entity):
    """
    馆室有没有某一服务
    """
    room = entity['room'][0]
    service = entity['service'][0]
    res = get_reverse_relation(room, '服务')
    ans = '\n'
    for r in res:
        if r['office_name'] == service:
            ans += room + '提供' + service + '\n'
            return ans

    ans += room + '不提供' + service + '\n'
    return ans


def solve_service_area_exit(entity):
    """
    馆室有没有某一服务
    """
    area = entity['area'][0]
    service = entity['service'][0]
    res = get_reverse_relation_mul(area, '服务')
    ans = '\n'
    for r in res:
        if r['office_name'] == service:
            ans += area + '提供' + service + '\n'
            return ans

    ans += area + '不提供' + service + '\n'
    return ans


def solve_open_room():
    """
    开架阅览室有哪些
    """
    res = get_entity('馆室')
    room_arr = []
    for r in res:
        if r['open'] == '1':
            room_arr.append(r['office_name'])
    ans = '\n开架阅览室包括'
    for sub_room in room_arr[:-1]:
        ans += sub_room + ','
    ans += room_arr[-1] + '\n'
    return ans
